package com.yohann.order_service.controller;

import com.yohann.order_service.entity.Orders;
import com.yohann.order_service.entity.vo.OrderVo;
import com.yohann.order_service.service.OrdersService;
import com.yohann.order_service.utils.OrderIdUtil;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Yohann
 * @since 2020-11-09
 */
@Api(tags = "订单")
@RestController
@RequestMapping("/order_service")
public class OrdersController {
    @Autowired
    private OrdersService orderService;

    /**
     * 创建订单
     *
     * @param order 订单信息
     * @return 创建状态
     */
    @ApiOperation("创建订单")
    @PostMapping("createOrder")
    public Result createOrder(@ApiParam("订单信息") @RequestBody Orders order) {
        //生成订单id
        order.setId(OrderIdUtil.getOrderNo());
        //设置创建时间
        order.setGmtCreate(new Date());
        orderService.save(order);

        return Result.ok();
    }

    /**
     * 直接支付成功
     *
     * @param order 订单信息
     * @return 创建状态
     */
    @ApiOperation("创建订单")
    @PostMapping("pay")
    public Result pay(@ApiParam("订单信息") @RequestBody Orders order) {
        orderService.pay(order);

        return Result.ok();
    }

    /**
     * 通过用户id查询获取订单信息
     *
     * @param id 用户id
     * @return 订单信息
     */
    @ApiOperation("通过用户id查询获取订单信息")
    @GetMapping("getOrderInfo/{id}")
    public Result getOrderInfo(@ApiParam("用户id") @PathVariable String id) {
        OrderVo orderVo = orderService.getOrderInfo(id);

        return Result.ok().data("orderVo", orderVo);
    }

    /**
     * 删除订单
     *
     * @param id 订单id
     * @return 删除状态
     */
    @ApiOperation("删除订单")
    @GetMapping("deleteOrder/{id}")
    public Result deleteOrder(@ApiParam("订单id") @PathVariable String id) {
        orderService.removeById(id);

        return Result.ok();
    }

    /**
     * 查询订单信息
     *
     * @param order 查询条件
     * @return 订单信息
     */
    @ApiOperation("查询订单信息")
    @PostMapping("getOrder/{current}/{limit}")
    public Result queryOrder(@ApiParam("起始位置") @PathVariable int current,
                             @ApiParam("结束位置") @PathVariable int limit,
                             @ApiParam("查询条件") @RequestBody(required = false) Orders order) {
        Map<String, Object> map = orderService.queryOrder(current, limit, order);

        return Result.ok().data(map);
    }

    /**
     * 获取用户的所有订单
     *
     * @param id 用户id
     * @return 订单集合
     */
    @ApiOperation("获取用户的所有订单")
    @GetMapping("getUserOrder/{id}")
    public Result getUserOrder(@ApiParam("用户id") @PathVariable int id) {
        List<OrderVo> list = orderService.getUserOrder(id);

        return Result.ok().data("list", list);
    }

    /**
     * 获取用户的所有订单
     */
    @ApiOperation("获取购买情况")
    @GetMapping("getPurchaseStatus/{userId}/{courseId}")
    public Result getPurchaseStatus(@ApiParam("用户id") @PathVariable int userId, @ApiParam("课程id") @PathVariable int courseId) {
        return Result.state(orderService.getPurchaseStatus(userId, courseId));
    }
}

